package com.baizhi.service.impl;

import com.baizhi.dao.SurveyDao;
import com.baizhi.entity.Survey;
import com.baizhi.service.SurveyService;
import com.baizhi.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class SurveyServiceImpl implements SurveyService {
	
	@Override
	public Survey selectOne(int id) {
		SqlSession sqlSession = null;
		Survey survey = null;
		try {
			sqlSession = MyBatisUtils.getSqlSession();
			SurveyDao sd=sqlSession.getMapper(SurveyDao.class);
			
			survey = sd.selectOne(id);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		}finally{
			MyBatisUtils.close(sqlSession);
		}
		return survey;
	}

	@Override
	public List<Survey> selectMySurvey(int usersid) {
		SqlSession sqlSession = null;
		List<Survey> surveyList = null;
		try {
			sqlSession = MyBatisUtils.getSqlSession();
			SurveyDao sd=sqlSession.getMapper(SurveyDao.class);
			surveyList = sd.selectMySurvey(usersid);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		}finally{
			MyBatisUtils.close(sqlSession);
		}
		return surveyList;
	}

	@Override
	public void insert(Survey survey) {
		SqlSession sqlSession = null;
		try {
			sqlSession = MyBatisUtils.getSqlSession();
			SurveyDao sd=sqlSession.getMapper(SurveyDao.class);
			sd.insert(survey);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		}finally{
			MyBatisUtils.close(sqlSession);
		}
	}

	@Override
	public List<Survey> findAllAvailableSurveys() {
		SqlSession sqlSession = null;
		List<Survey> allAvailableSurveys = null;
		try {
			sqlSession = MyBatisUtils.getSqlSession();
			SurveyDao sd=sqlSession.getMapper(SurveyDao.class);
			allAvailableSurveys = sd.findAllAvailableSurveys();
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		}finally{
			MyBatisUtils.close(sqlSession);
		}
		return allAvailableSurveys;
	}

	@Override
	public Survey selectOnlySurvey(int id) {
		SqlSession sqlSession = null;
		Survey survey = null;
		try {
			sqlSession = MyBatisUtils.getSqlSession();
			SurveyDao sd=sqlSession.getMapper(SurveyDao.class);
			survey=sd.selectOnlySurvey(id);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		}finally{
			MyBatisUtils.close(sqlSession);
		}
		return survey;
	}
}
